package org.apache.webapp.balancer.rules;

import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;

/**
 * This is the base class for the custom load balancing rules
 * defined to redirect the web requests to a specific servlet
 * container instance in the cluster.
 *
 * @author Srini Penchikala
 */
public abstract class BaseLoadBalancingRule extends BaseRule {

   // Variables defined in configuration XML file.
   private static int clusterMember = 1;
   private String serverInstance;
	private String maxServerInstances;
   private String tcpListenAddress;
   private String tcpListenPort;
   private String testWebPage;

	public int getClusterMember() {
		return clusterMember;
	}

	public void setClusterMember(int theClusterMember) {
		clusterMember = theClusterMember;
	}

   /**
    * Sets the serverInstance attribute.
    *
    * @param theServerInstance The target string
   */
   public void setServerInstance(String theServerInstance) {
      if (theServerInstance == null) {
         throw new IllegalArgumentException(
         "The target string cannot be null.");
      } else {
         serverInstance = theServerInstance;
      }
   }

   /**
    * Returns the serverInstance attribute.
    *
    * @return The serverInstance
    */
   protected String getServerInstance() {
      return serverInstance;
   }

   public String getMaxServerInstances() {
     return maxServerInstances;
   }

   public void setMaxServerInstances(String theMaxServerInstances) {
     maxServerInstances = theMaxServerInstances;
   }

   public String getTcpListenAddress() {
      return tcpListenAddress;
   }

   public void setTcpListenAddress(String theTcpListenAddress) {
      tcpListenAddress = theTcpListenAddress;
   }

   public String getTcpListenPort() {
      return tcpListenPort;
   }

   public void setTcpListenPort(String theTcpListenPort) {
      tcpListenPort = theTcpListenPort;
   }

   public String getTestWebPage() {
      return testWebPage;
   }

   public void setTestWebPage(String theTestWebPage) {
      testWebPage = theTestWebPage;
   }

   public boolean isServerAlive(String host, int port) {
      common.utilities.ServerUtil test = new common.utilities.ServerUtil();
      boolean isAlive = false;
      isAlive = test.isServerAlive(host, port);
      if (isAlive) System.out.println("**  IT'S ALIVE  **");
      else         System.out.println("**  THE SERVER IS NOT AVAILABLE  **");
      return isAlive;
   }

   public boolean isServerAlive(String webpage) {
      common.utilities.ServerUtil test = new common.utilities.ServerUtil();
      boolean isAlive = false;
      isAlive = test.isServerAlive(webpage);
      if (isAlive) System.out.println("**  IT'S ALIVE  **");
      else         System.out.println("**  THE SERVER IS NOT AVAILABLE  **");
      return isAlive;
   }

}
// End of BaseLoadBalancingRule.java
